import indexObj from '../../unit/index.js';
import event from '../../unit/event';
import states from '../states';
import musicobj from '../../unit/music';
import Block from '../../unit/block.js';
let {
	want
} = indexObj;
let {
	music
} = musicobj;

const down = (store) => {
	store.commit("rotate", true);
	if (store.state.cur !== null) {
		event.down({
			key: 'rotate',
			once: true,
			callback: () => {
				const state = store.state;
				if (state.lock) {
					return;
				}
				if (state.pause) {
					states.pause(false);
				}
				const cur = state.cur;
				if (cur === null) {
					return;
				}
				if (music.rotate) {
					music.rotate();
				}
				const next = cur.rotate();
				if (want(next, state.matrix)) {
					// store.dispatch(actions.moveBlock(next));
					store.commit("moveBlock", next.reset === true ? null : new Block(next));
				}
			},
		});
	} else {
		event.down({
			key: 'rotate',
			begin: 200,
			interval: 100,
			callback: () => {
				if (store.state.lock) {
					return;
				}
				if (music.move) {
					music.move();
				}
				const state = store.state;
				const cur = state.cur;
				if (cur) {
					return;
				}
				let startLines = state.startLines;
				startLines = startLines + 1 > 10 ? 0 : startLines + 1;
				// store.dispatch(actions.startLines(startLines));
				store.commit("startLines", startLines);
			},
		});
	}
};

const up = (store) => {
	store.commit("rotate", false);
	event.up({
		key: 'rotate',
	});
};

export default {
	down,
	up,
};